<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 模拟tab产生空格
 * @param int $step
 * @param string $string
 * @param int $size
 * @return string
 */
function tab($step = 1, $string = ' ', $size = 4)
{
    return str_repeat($string, $size * $step);
}

/**
 * 数据签名认证
 * @param  array $data 被认证的数据
 * @return string       签名
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function data_auth_sign($data)
{
    //数据类型检测
    if (!is_array($data)) {
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}

/**
 * 对查询结果集进行排序
 * @access public
 * @param array $list 查询结果
 * @param string $field 排序的字段名
 * @param array $sortBy 排序类型
 *                      asc正向排序 desc逆向排序 nat自然排序
 * @return array|bool
 */
function list_sort_by($list, $field, $sortBy = 'asc')
{
    if (is_array($list)) {
        $refer = $resultSet = [];
        foreach ($list as $i => $data)
            $refer[$i] = &$data[$field];
        switch ($sortBy) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc': // 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ($refer as $key => $val)
            $resultSet[] = &$list[$key];

        return $resultSet;
    }

    return false;
}

/**
 * 模板搜索排序
 * @param string $name 单元格名称
 * @param string $field 排序字段
 * @return string
 */
function sort_by($name, $field = '')
{
    return '<a href="javascript:sort(\'' . $field . '\');">' . $name . '</a>';
}

/**
 * 模板状态显示
 * @param int $status
 * @param bool $imageShow true只显示图标|false只显示文字
 * @return string
 */
function get_status($status, $imageShow = true)
{
    switch ($status) {
        case 1 :
            $showText = '<label class="label label-success">启用</label>';
            $showImg = '<i class="fa fa-check text-success" title="启用"></i>';
            break;
        case 0 :
            $showText = '<label class="label label-danger">禁用</label>';
            $showImg = '<i class="fa fa-times text-danger" title="禁用"></i>';
            break;
        default :
            $showText = '<label class="label label-danger">禁用</label>';
            $showImg = '<i class="fa fa-times text-danger" title="禁用"></i>';

    }
    return ($imageShow === true) ? $showImg : $showText;
}

/**
 * 单向加密
 * @param $str
 * @return string
 */
function encrypt($str)
{
    return md5(crypt($str, think\Config::get('secret_key')));
}

/*
 * 生成菜单
 */
function get_menu($cate, $lefthtml = '-- ', $pid = 0, $lvl = 0, $leftpin = 0)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $v['lvl'] = $lvl + 1;
            $v['leftpin'] = $leftpin + 0;//左边距
            $v['lefthtml'] = str_repeat($lefthtml, $lvl);
            $arr[] = $v;
            $arr = array_merge($arr, get_menu($cate, $lefthtml, $v['id'], $lvl + 1, $leftpin + 20));
        }
    }
    return $arr;
}

/*
 * 管理员操作日志
 */
function adminLog($log_info){
    $user_auth = think\Session::get('user_auth');
    $data['log_time'] = time();
    $data['uid'] = $user_auth['uid'];
    $data['user_name'] = $user_auth['user_name'];
    $data['log_info'] = $log_info;
    $data['ip_address'] = think\Request::instance()->ip();
    $ip = new IpLocation();
    $data['location'] = $ip->getipaddress();
    think\Db::name('admin_log')->insert($data);
}